package com.baidu.servlet;

import com.baidu.entity.AirBean;
import com.baidu.entity.DistrictBean;
import com.baidu.entity.GoodsBean;
import com.baidu.entity.SortBean;
import com.baidu.service.AirService;
import com.baidu.service.GoodsService;
import com.baidu.utils.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 创作时间：2019/9/19 8:41
 * 作者：李增强
 */
@WebServlet("/goodsServlet")
public class GoodsServlet extends HttpServlet {
    GoodsService gs = new GoodsService();

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("list".equals(method)){
            getGoodsList(req,resp);
        }else if("tobuy".equals(method)){
            toBuyGoods(req,resp);
        }else if("buy".equals(method)){
            buyGoods(req,resp);
        }
    }
    public void buyGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        String id = req.getParameter("id");
        String buycount = req.getParameter("buycount");
        boolean flag = gs.updateGoods(buycount,id);
        String msg="购买失败";
        if(flag){
            msg="购买成功";
        }
        req.setAttribute("msg", msg);
        getGoodsList(req, resp);
    }
    public void toBuyGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        String id = req.getParameter("id");
        GoodsBean gb = gs.getGoodsById(id);
        req.setAttribute("gb", gb);
        req.getRequestDispatcher("/WEB-INF/pages/goods/goods_buy.jsp").forward(req, resp);
    }

    public void getGoodsList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        String sortid = req.getParameter("sortid");
        List<GoodsBean> list = gs.getGoodsList(sortid);
        List<SortBean> slist = gs.getSortList();
        req.setAttribute("slist", slist);
        req.setAttribute("sortid", sortid);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/WEB-INF/pages/goods/goods_list.jsp").forward(req, resp);
    }

}
